const express = require('express');
const jwt = require('jsonwebtoken');
const bodyParser = require('express.json');
const app = express();
app.use(bodyParser());

// 模拟用户数据
const users = [
    { username: "test", password: "test", id: 1 },
];

// 登录接口
app.post('/api/login', (req, res) => {
    const { username, password } = req.body;
    const user = users.find(u => u.username === username && u.password === password);
    if (user) {
        const token = jwt.sign({ userId: user.id, username }, 'your-secret-key', { expiresIn: '1h' });
        res.json({ token });
    } else {
        res.status(401).json({ message: '用户名或密码错误' });
    }
   
});

const port = 5174;
app.listen(port, () => {
    console.log(`Server running on port ${port}`);
});